package org.jacp.demo.dao;

import java.util.List;

import org.jacp.demo.entities.Employee;
import org.jacp.demo.services.EmployeeService;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class EmployeeDAO extends JpaDaoSupport implements EmployeeService {

    public Employee findById(long id) {
	return getJpaTemplate().find(Employee.class, id);
    }

    public List<Employee> findAll() {
	return getJpaTemplate().find("select e from Employee e");
    }

    public List<Employee> findByEmployeeNumber(String empno) {
	return getJpaTemplate().find(
		"select e from Employee e where e.empno = ?1", empno);
    }

    public List<Employee> findByAddressStreetName(String street) {
	return getJpaTemplate().find(
		"select e from Employee e where e.addr.street = ?1", street);
    }

    public List<Employee> findByEmployeeLastName(String lastName) {
	return getJpaTemplate().find(
		"select e from Employee e where e.lastName = ?1", lastName);
    }
    
    public List<Employee> findByEmployeeName(String name) {
	return getJpaTemplate().find(
		"select e from Employee e where e.firstName = ?1", name);
    }

    public List<Employee> findEmployeeWithSalaryOver(double sal) {
	return getJpaTemplate().find(
		"select e from Employee e where e.salary > ?1", sal);
    }

    public List<Employee> findEmployeeWithCommissionOver(double comm) {
	return getJpaTemplate().find(
		"select e from Employee e where e.commission > ?1", comm);
    }

    @Transactional
    public Employee save(Employee emp) {

	getJpaTemplate().persist(emp);
	getJpaTemplate().flush();
	return emp;
    }

    @Transactional
    public Employee update(Employee emp) {
	return getJpaTemplate().merge(emp);
    }

    @Transactional
    public void delete(Employee emp) {
	getJpaTemplate().remove(emp);

    }

}
